package com.shujia;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class JedisCode {

    String ip = "192.168.253.80";
    int port = 6379;

    @Test
    public void stringGetSet() {
        Jedis jedis = new Jedis(ip, port);
        jedis.set("apiString", "apiValue");
        String apiString = jedis.get("apiString");
        System.out.println(apiString);
    }

    /**
     * Jedis操作Redis中的List数据类型
     */
    @Test
    public void ListGetSet() {
        Jedis jedis = new Jedis(ip, port);
        jedis.lpush("apiList", "value1", "value2", "value3");
        List<String> apiList = jedis.lrange("apiList", 0, -1);
        for (String value : apiList) {
            System.out.println(value);
        }
    }

    /**
     * Set
     */

    @Test
    public void setGetSet() {
        Jedis jedis = new Jedis(ip, port);
        jedis.sadd("apiSet", "setValue1", "setValue2", "setValue3");
        Set<String> apiSet = jedis.smembers("apiSet");
        for (String value : apiSet) {
            System.out.println(value);
        }
    }

    /**
     * Hash
     */
    @Test
    public void hashGetSet() {
        Jedis jedis = new Jedis(ip, port);
        jedis.hset("apiObj", "name", "zhangsan");
        jedis.hset("apiObj", "age", "18");
        jedis.hset("apiObj", "gender", "男");

        String name = jedis.hget("apiObj", "name");
        String age = jedis.hget("apiObj", "age");
        String gender = jedis.hget("apiObj", "gender");

        System.out.println("name:" + name + " age:" + age + " gender" + gender);
    }

    @Test
    public void hashGetSet2() {
        Jedis jedis = new Jedis(ip, port);
        HashMap<String, String> filedAndValue = new HashMap<>();
        filedAndValue.put("name","wangwu");
        filedAndValue.put("age","23");
        filedAndValue.put("gender","女");
        jedis.hmset("apiObj2",filedAndValue);
    }

}
